package com.furongji.education.controller.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.furongji.common.domain.vo.CourseOrderVo;
import com.furongji.common.domain.vo.VideoPlayPageVo;
import com.furongji.common.result.Result;
import com.furongji.education.domain.po.Course;
import com.furongji.education.domain.query.CourseFrontQuery;
import com.furongji.education.domain.vo.CourseFrontVo;
import com.furongji.education.service.CourseService;
import com.furongji.education.service.TeacherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Tag(name = "用户端课程管理")
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/courses")
public class CourseApiController {

    private final CourseService courseService;

    private final TeacherService teacherService;

    @Operation(summary = "用户端课程列表")
    @PostMapping("/getFrontCourseList/{current}/{size}")
    public Result getFrontCourseList(@PathVariable Integer current, @PathVariable Integer size,
                                     @RequestBody CourseFrontQuery courseFrontQuery) {
        IPage<Course> page = new Page<>(current, size);
        IPage<Course> result = courseService.getApiCourseList(page, courseFrontQuery);
        return Result.success(result);
    }

    // TODO 后续从本地线程池中取userId
    @Operation(summary = "用户端课程详情")
    @GetMapping("/getFrontCourseInfo/{courseId}")
    public Result getFrontCourseInfo(@PathVariable String courseId,
                                     @RequestParam(required = false) String userId) {
        System.err.println("课程Id：" + courseId + "  用户Id：" + userId);
        CourseFrontVo courseFrontVo = courseService.getApiCourseInfo(courseId, userId);
//        System.err.println(courseFrontVo);
        return Result.success(courseFrontVo);
    }

    @Operation(summary = "根据讲师Id查询相关课程")
    @GetMapping("/getCourseInfoByTeacherId/{teacherId}")
    public Result getCourseInfoByTeacherId(@PathVariable String teacherId) {
        List<Course> courseList = courseService.getApiCourseInfoByTeacherId(teacherId);
        return Result.success(courseList);
    }

    // feign
    @Operation(summary = "根据课程id返回课程信息")
    @GetMapping("/getCourserOrderInfo/{courserId}")
    public CourseOrderVo getCourserOrderInfo(@PathVariable String courserId) {
        Course course = courseService.getById(courserId);
        String teacherName = teacherService.getById(course.getTeacherId()).getName();
        CourseOrderVo courseOrderVo = new CourseOrderVo(course.getId(), course.getTitle(),
                course.getCover(), course.getPrice(), teacherName);

        return courseOrderVo;
    }

    // feign
    @Operation(summary = "获取视频播放页面信息")
    @GetMapping("/getCourseInfoWithSections/{videoId}")
    public VideoPlayPageVo getCourseInfoWithSections(@PathVariable String videoId) {
        VideoPlayPageVo videoPlayPageVo = courseService.getCourseInfoWithSections(videoId);
        System.err.println("videoPlayPageVo: " + videoPlayPageVo);
        return videoPlayPageVo;
    }
}
